package jzbank.util;



import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Slf4j
public class FileUtil {


    private FileUtil() {

    }


    public static List<String> getFileList(String dir, String fileType)
    {
        List<String> txtList = new ArrayList<String>();

        File file = new File(dir);
        File[] files = file.listFiles();

        if (isDir(file))
        {
            for (File tempFile : files)
            {
                if (tempFile.getAbsolutePath().endsWith(fileType))
                    txtList.add(tempFile.getAbsolutePath());
            }
            log.debug("" + file.getAbsolutePath() + "" + "" + txtList.size() + "]");
        }
        return txtList;
    }



    public static void fileCopy(String sourcePath, String targetPath) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        int byteRead = 0;
        byte[] buffer = new byte[1024];
        try {
            fis = new FileInputStream(sourcePath);
            fos = new FileOutputStream(targetPath);
            try {
                while ((byteRead = fis.read(buffer)) != -1) {
                    fos.write(buffer, 0, byteRead);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            safeCloseFileOutputStream(fos);
            safeCloseFileInputStream(fis);
        }
    }

    public static void fileDelete(String path) {
        File delFile = new File(path);
        if (delFile.exists())
            delFile.delete();
    }


    public static void fileMove(String sourcePath, String targetPath) {
        fileCopy(sourcePath, targetPath);
        fileDelete(sourcePath);
    }


    public static boolean isDir(File file) {

        if (file.exists()) {
            if (file.isDirectory())
                return true;
            log.debug("-> [" + file.getAbsolutePath() + "");
            return false;
        }
        log.debug("-> [" + file.getAbsolutePath() + "");
        return false;

    }

    public static void creatLogDirectory()
    {
        File log = new File("log");
        File whole = new File("log/syncwhole");

        if ((!(log.exists())) || (!(log.isDirectory())))
            log.mkdirs();
        else {
            log = null;
        }
        if ((!(whole.exists())) || (!(whole.isDirectory())))
            whole.mkdirs();
        else {
            whole = null;
        }
    }

    private static void safeCloseFileInputStream(FileInputStream fis) {
        if (null != fis) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    private static void safeCloseFileOutputStream(FileOutputStream fos) {
        if (null != fos) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @SuppressWarnings("unused")
    private static void safeCloseBufferedWriter(BufferedWriter bw)
    {
        if (null != bw)
        {
            try
            {
                bw.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    public static void createDir(String dir) {
        StringBuffer dateStrDirSb = new StringBuffer(dir);
        dateStrDirSb.append("/");
        File dateStrDir = new File(dateStrDirSb.toString());

        if (!dateStrDir.exists() || !dateStrDir.isDirectory())
        {
            dateStrDir.mkdirs();
        }
    }


    public static String[] sortByStrLength(String[] arrStr) {
        String temp;
        for (int i = 0; i < arrStr.length; i++) {
            for (int j = arrStr.length - 1; j > i; j--) {
                if (arrStr[i].length() < arrStr[j].length()) {
                    temp = arrStr[i];
                    arrStr[i] = arrStr[j];
                    arrStr[j] = temp;
                }
            }
        }
        return  arrStr;
    }

    public static String PriorAddHeadZero(String Str, int Length) {
        String ResultValue = Str;
        while (ResultValue.length() < Length) {
            ResultValue = "0" + ResultValue;
        }
        return ResultValue;
    };

    public static String newDate(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        String newdate=sdf.format(new Date());
        return newdate;
    }

    public static String yesterDay(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        String yesterday=new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
        return yesterday;
    }

    public static String yesterDayTozd(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        String yesterday=new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return yesterday;
    }


    public static String nowMonthOneDay(){
        SimpleDateFormat sd=new SimpleDateFormat("yyyyMMdd");
        Calendar c = Calendar.getInstance();
        int month=c.get(Calendar.MONTH);
        c.set(Calendar.MONTH, month-1);
        c.set(Calendar.DAY_OF_MONTH,c.getActualMinimum(Calendar.DAY_OF_MONTH));
        String date=sd.format(c.getTime());
        return date;
    }

    public static int StrLen(String Des) {
        return Des.getBytes().length;
    }


    public static String SubStr(String Des, int b) throws Exception {
        return SubStr(Des, b, StrLen(Des));
    };

    public static String SubStr(String Des, int b, int e) throws Exception {
        byte[] buff = new byte[e - b + 1];
        buff = myCopyArray(Des.getBytes(), b, e);
        return new String(buff);
    };


    public static byte[] myCopyArray(byte[] buff, int beginIndex, int endIndex)
            throws Exception {
        if (endIndex - beginIndex < 0) {
            throw new Exception("");
        }
        if (endIndex > (buff.length)) {
            throw new Exception("");
        }
        byte[] _buff = new byte[endIndex - beginIndex];
        System.arraycopy(buff, beginIndex, _buff, 0, _buff.length);
        return _buff;
    }

    public static List<String> delPath(String path){
        List<String> dellist = new ArrayList<String>();

        return delPath(path,dellist);
    }
    public static List<String> delPath(String path,List<String> dellist){
        File rootfile = new File(path);

        if(rootfile.exists() && rootfile.isDirectory()){
            String[] listFiles = rootfile.list();
            if(listFiles.length == 0){
                if(rootfile.delete()){
                    dellist.add(rootfile.getAbsolutePath());
                }
            }else{
                for (int i = 0; i < listFiles.length; i++) {
                    String a= path + "/" + listFiles[i];
                    delPath(a,dellist);
                }
            }
        }else if(rootfile.exists() && rootfile.isFile()){
            if(rootfile.delete()){
                dellist.add(rootfile.getAbsolutePath());
            }
        }
        rootfile = null;
        return dellist;
    }
}

